package com.renhuikeji.pro.ala.utils;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.lzy.okgo.model.Response;
import com.renhuikeji.pro.ala.R;
import com.renhuikeji.pro.ala.bean.BasicBean;
import com.renhuikeji.pro.ala.config.Constant;


import java.lang.reflect.Type;

/**
 * Created by Administrator on 2017/11/2.
 */

public class DealResUtil {

    private static DealResUtil instace=new DealResUtil();

    public static DealResUtil getInstace(){
        return  instace;
    }
    public DealResUtil() {

    }

    public  <T>T dealData(Context context,Response<String> response , Class<T> obj){
        T object=null;
        String res=response.body().toString();
        if(TextUtils.isEmpty(res)){
            ToastUtils.toastForShort(context,context.getString(R.string.empty_datas));
            return object;
        }
        try {
                object=new Gson().fromJson(res,obj);
            return object;
        }catch (Exception e){
            ToastUtils.toastForShort(context,context.getString(R.string.analysis_error));
            e.printStackTrace();
            return object;
        }
    }
    public  <T>T dealRes(Context context, Response<String> response , Type type){
       T object=null;
        String res=response.body().toString();
        if(TextUtils.isEmpty(res)){
            ToastUtils.toastForShort(context,context.getString(R.string.empty_datas));
            return object;
        }
        try {
             BasicBean<T> bean=new Gson().fromJson(res,type);
            if(bean.getStatus()==Constant.SUCCESS){
                return  bean.getData();
            }else{
                ToastUtils.toastForShort(context,bean.getMsg());
                return object;
            }
        }catch (Exception e){
            ToastUtils.toastForShort(context,context.getString(R.string.analysis_error));
            e.printStackTrace();
            return object;
        }

    }
    public BasicBean  dealBase(Context context,Response<String> response){
        BasicBean bean=null;
        String res=response.body().toString();
        if(TextUtils.isEmpty(res)){
            ToastUtils.toastForShort(context,context.getString(R.string.empty_datas));
            return bean;
        }
         try {
              bean=new Gson().fromJson(res,BasicBean.class);
             if(bean!=null){
                 ToastUtils.toastForShort(context,bean.getMsg());
             }
             return bean;
         }catch (Exception e){
             ToastUtils.toastForShort(context,context.getString(R.string.analysis_error));
             e.printStackTrace();
             return bean;
         }
    }
}
